﻿using System;
using UnityEngine;

namespace Core
{
    /// <summary>
    /// 多Render 模型组件
    /// </summary>
    public class MultiVisualComponent : Core.Component, IGameObject, IAwake<string>
    {
        public GameObject gameObject { get; private set; }
        public Transform transform { get; private set; }
        public bool IsLoadComplete { get; private set; }

        private event Action OnLoaded;

        private string prefabPath;
        private float offsetY;
        private IGameObject unit;

        private SpriteRenderer[] renderes;

        public void Awake(string path)
        {
            Awake(path, 0);
        }

        public void Awake(string path, float value)
        {
            prefabPath = path;
            offsetY = value;
            Name = "model";

            unit = Entity as IGameObject;

            if (unit == null)
            {
                log.err($"ModelComponent 需要挂载在 IGameObject对象上");
            }

            IsLoadComplete = false;
            Load().Coroutine();
        }

        private void Awake(GameObject go)
        {
            IsLoadComplete = true;
            gameObject = go;
            transform = gameObject.transform;
            InitRender();

            transform.SetParent(unit);
            transform.SetLayer(unit.gameObject.layer);
            transform.Reset();

            transform.SetLocalPosY(offsetY);
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            mSortingOrder = 0;

            transform = null;
            Utils.Trans.Destroy(gameObject);
            gameObject = null;
        }

        private void InitRender()
        {
            renderes = gameObject.GetComponentsInChildren<SpriteRenderer>();
            if (renderes != null)
            {
                mSortingOrder = renderes[0].sortingOrder;
            }
        }

        private int mSortingOrder;
        public int SortingOrder
        {
            get => mSortingOrder;
            set
            {
                mSortingOrder = value;
                if (renderes != null)
                {
                    foreach (var render in renderes)
                    {
                        render.sortingOrder = value;
                    }
                }
            }
        }


        private async ETVoid Load()
        {
            var asset = await G.Res.LoadAsync(prefabPath);

            var go = UnityEngine.Object.Instantiate(asset.Obj) as GameObject;

            Awake(go);

            try
            {
                OnLoaded?.Invoke();
            }
            catch (Exception e)
            {
                log.err(e);
            }

            IsLoadComplete = true;
        }

        public void AddLoadFunc(Action func)
        {
            if (IsLoadComplete == false)
            {
                OnLoaded += func;
            }
            else
            {
                func();
            }
        }

        public void RemoveLoadFunc(Action func)
        {
            OnLoaded -= func;
        }
    }
}
